<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>E键配</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <link href="./static/plugins/bootstrap/bootstrap.min.css" rel="stylesheet">
    <link href="./static/css/font-awesome.min.css" rel="stylesheet">
    <link href="./static/plugins/toastr/toastr.min.css" rel="stylesheet">
    <link href="./static/plugins/ladda/ladda-themeless.min.css" rel="stylesheet">
    <link href="./static/css/animate.css" rel="stylesheet">
    <link href="./static/css/style.css" rel="stylesheet">

    <script type="text/javascript" src="./static/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" href="./static/plugins/bootstrap/bootstrap.min.js"></script>
    <script type="text/javascript" src="./static/plugins/toastr/toastr.min.js"></script>
    <script type="text/javascript" src="./static/plugins/validate/jquery.validate.min.js"></script>
    <script type="text/javascript" src="./static/plugins/ladda/spin.min.js"></script>
    <script type="text/javascript" src="./static/plugins/ladda/ladda.min.js"></script>
    <script type="text/javascript" src="./static/plugins/ladda/ladda.jquery.min.js"></script>
    <script type="text/javascript" src="./static/js/vue.min.js"></script>
    <script type="text/javascript" src="./static/js/main.js"></script>
    <script>
        if (token && token.length > 30) {
            location.href = '/home.html';
        }
    </script>
</head>
<body class="gray-bg">
<div class="middle-box text-center reg-page animated fadeInDown">
    <div>
        <div>
            <img class="logo-name"
                 src=""
                 alt="E键配">
        </div>
        <!--<h3>E键配</h3>-->
        <p class="m-t">郑州畅配网络科技有限公司</p>
        <form class="m-t m-b form-horizontal" role="form" id="theForm">
            <div class="form-group">
                <label class="col-md-3 control-label">会员类别</label>
                <div class="col-md-9">
                    <select class="form-control" name="type" v-model="type">
                        <option value="0">请选择</option>
                        <option value="1">修理厂</option>
                        <option value="2">配件商</option>
                        <option value="3">保险公司</option>
                    </select>
                </div>
            </div>
            <div>
                <div class="form-group" v-if="type==3">
                    <label class="col-md-3 control-label">保险公司</label>
                    <div class="col-md-9">
                        <select class="form-control" name="insurerBrand_id" v-model="inquiry_id" @change="selectMulti('inquiry','province')">
                            <option value="0">请选择保险品牌</option>
                            <option :value="vo.id" v-for="(vo,index) in inquiry" :data-idx="index">{{vo.value}}</option>
                        </select>
                    </div>
                    <div class="col-md-9 col-md-offset-3 m-t" v-show="inquiry_id>0">
                        <select class="form-control" name="insurerProvince_id" v-model="province_id" @change="selectMulti('province','city')">
                            <option value="0">请选择省公司</option>
                            <option :value="vo.id" v-for="(vo,index) in province" :data-idx="index">{{vo.value}}</option>
                        </select>
                    </div>
                    <div class="col-md-9 col-md-offset-3 m-t" v-show="province_id>0">
                        <select class="form-control" name="insurerCity_id" v-model="city_id" @change="selectMulti('city')">
                            <option value="0">请选择市公司</option>
                            <option :value="vo.id" v-for="(vo,index) in city" :data-idx="index">{{vo.value}}</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">手机号</label>
                <div class="col-md-9">
                    <div class="input-group">
                        <input type="text" name="phone" class="form-control" placeholder="请输入手机号">
                        <span class="input-group-btn">
                            <button type="button" id="verify-btn" class="ladda-button btn btn-disabled" data-style="expand-right"><small>发送验证码</small></button>
                        </span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">验证码</label>
                <div class="col-md-9">
                    <input type="text" name="smscode" class="form-control" placeholder="请输入验证码">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">密码</label>
                <div class="col-md-9">
                    <input type="password" name="password" id="password" class="form-control" placeholder="请输入密码">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">重复密码</label>
                <div class="col-md-9">
                    <input type="password" name="repassword" class="form-control" placeholder="请再次输入密码">
                </div>
            </div>
            <input type="hidden" name="from" value="pc"/>
            <div class="clearfix"></div>
            <div class="m-t">
                <button type="submit" class="ladda-button btn btn-success block full-width m-b" data-style="expand-right" id="reg-btn">注&nbsp;册</button>

                <a class="btn btn-sm btn-primary btn-outline btn-block" href="/">去登录</a>
            </div>
            <div class="clearfix"></div>
        </form>
    </div>
</div>

<script>
    var vm = new Vue({
        el: '#theForm',
        data: {
            type: 0,
            inquiry_id: 0,//已选择的保险公司
            inquiry: [],//保险公司
            province_id: 0,
            province: [],
            city_id: 0,
            city: [],
        },
        methods: {
            selectMulti: function (name, child) {
                var obj = $(event.currentTarget);
                var index = obj.find("option[value=" + vm[(name + '_id')] + "]").data('idx');

                if (child && index == undefined) {
                    vm[child] = [];
                    vm[(child + '_id')] = 0;
                    return;
                }
                if (child) {
                    vm[child] = vm[name][index].childs;
                    vm[child + '_id'] = 0;
                }
            },
        },
        created: function () {
            var instance = this;
            $.ajax({
                type: 'POST',
                url: api + '/index/insurerTree',
                success: function (data) {
                    instance.inquiry = data;
                }
            });
        }
    });
    $(function () {
        $('#theForm').validate({
            rules: {
                type: {
                    range: [1, 3],
                },
                smscode: {
                    required: true,
                },
                password: {
                    required: true,
                },
                repassword: {
                    equalTo: '#password',
                }
            },
            messages: {
                type: {
                    range: '<i class="icon icon-exclamation-sign"></i>请选择会员类别'
                },
                smscode: {
                    required: '<i class="icon icon-exclamation-sign"></i>请输入验证码'
                },
                password: {
                    required: '<i class="icon icon-exclamation-sign"></i>密码不能为空',
                },
                repassword: {
                    equalTo: '<i class="icon icon-exclamation-sign"></i>两次输入密码不一致'
                }
            },
            submitHandler: function (form) {
                if (vm.type == 3 && vm.inquiry_id < 1) {
                    toastr.warning('请选择保险公司');
                    return;
                }
                if (!checkMobile($('input[name="phone"]').val())) {
                    toastr.warning('手机号码格式不正确');
                    return;
                }
                var btn = $('#reg-btn').ladda().ladda('start');
                $.ajax({
                    type: 'POST',
                    url: api + "index/register",
                    data: $(form).serialize(),
                    dataType: 'json',
                    success: function (data) {
                        btn.ladda('stop');
                        console.log(data);
                        if (data.ret == 0) {
                            setItem('token', data.data.token);
                            location.href = "/home.html";
                        } else {
                            toastr.error(data.msg);
                        }
                    }
                });
            }
        });

        $('input[name="phone"]').on('keyup', function () {
            if (checkMobile(this.value)) {
                $('#verify-btn').removeClass('btn-disabled').addClass('btn-primary');
            } else {
                $('#verify-btn').removeClass('btn-primary').addClass('btn-disabled');
            }
        });

        $('#verify-btn').on('click', function () {
            var btn = $(this);
            if (btn.hasClass('btn-disabled') || btn.hasClass('pendding')) {
                return;
            }
            var phone = $('input[name="phone"]').val();
            btn.ladda().ladda('start');
            $.ajax({
                type: 'POST',
                url: api + 'index/sendSms',
                data: {phone: phone},
                success: function (data) {
                    btn.ladda('stop');
                    counter();
                }
            });
        });
    });
    var handle;

    function counter() {
        var times = 30;
        var btn = $('#verify-btn');
        btn.removeClass('btn-primary').addClass('pendding').find('small').text('已发送');
        handle = setInterval(function () {
            if (times == 0) {
                btn.removeClass('pendding').addClass('btn-primary').find('small').text('重新发送');
                clearInterval(handle);
            } else {
                times--;
                btn.find('small').text('已发送(' + times + ')');
            }
        }, 1000);
    }

</script>
</body>
</html>
